package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketFootprint;
import com.cskaoyan.market.db.domain.MarketFootprintExample;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.service.AdminFootPrintService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/7/24
 * Time: 20:57
 * Description:
 */
public class AdminFootPrintServiceImpl implements AdminFootPrintService {
    @Override
    public List<MarketFootprint> list(String page, String limit, String userId, String goodsId, String sort, String order) {
        // 1. 获取sqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketFootprintMapper mapper = session.getMapper(MarketFootprintMapper.class);
        // 2. 增加筛选条件
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        // 用户Id(userId)非空时查询
        if(!StringUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        // 商品Id(goodsId)不为空时查询
        if(!StringUtils.isEmpty(goodsId)){
            criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
        }
        // 3. 增加排序方式
        marketFootprintExample.setOrderByClause(sort+" "+order);
        // 4. 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        List<MarketFootprint> marketFootprints = mapper.selectByExample(marketFootprintExample);
        // 5. 提交事务
        session.commit();
        session.close();
        // 6. 返回list给Controller
        return marketFootprints;

    }
}
